var express = require('express')
var fs = require('fs')
var path = require('path')

var app = express()

app.use(express.static('static'))

app.use(express.json())
app.use(express.urlencoded({extended:false}))

let lujing
let conment
function read (){
    lujing = path.join(__dirname , './json.json')
    conment = fs.readFileSync(lujing)
    conment = conment.toString() ? JSON.parse(conment) : []
}

//请求数据
app.post('/user/read' , (req , res) => {
    read()
    conment.push(...req.body)
    fs.writeFileSync(lujing , JSON.stringify(conment) , 'utf8')
})

//删除
app.post( '/user/clear' , (req , res) => {
    read()
    var {id} = req.body
        var index = conment.findIndex((item) => {
            return item.id == id
        })
        conment.splice(index , 1)
        res.send({data:conment})
        fs.writeFileSync(lujing , JSON.stringify(conment) , 'utf8')
})